package category

import (
	"github.com/gin-gonic/gin"
	"go-interview-forum/resp"
	"go-interview-forum/service/category"
	"strconv"
)

// 全部分类
func GetCategoryList(c *gin.Context) {
	list, err := category.GetCategoryList()
	if err != nil {
		resp.ServerErrorResp(c, resp.QUERY_ERR, "fail to get cate list")
		return
	}

	resp.SuccessResp(c, list)
}

// 获取任意分类等级列表
func GetAnyLevelCateList(c *gin.Context) {
	id := c.Query("id")
	level := c.Query("level")
	if level == "" {
		resp.ServerErrorResp(c, resp.QUERY_ERR, "get data error")
		return
	}
	ID := 0
	var err error
	if id != "" {
		ID, err = strconv.Atoi(id)
		if err != nil {
			resp.ServerErrorResp(c, resp.QUERY_ERR, "get data error")
			return
		}
	}

	Level, err := strconv.Atoi(level)
	if err != nil {
		resp.ServerErrorResp(c, resp.QUERY_ERR, "get data error")
		return
	}
	if Level != 2 && Level != 1 {
		resp.ServerErrorResp(c, resp.QUERY_ERR, "get data error")
		return
	}

	list, err := category.GetAnyLevelCate(ID, Level)
	if err != nil {
		resp.ServerErrorResp(c, resp.SERVER_ERR, "get data error")
		return
	}

	resp.SuccessResp(c, list)
}

// 根据名称搜索分类
func GetCateByCateName(c *gin.Context) {
	cname := c.Query("name")

	if cname == "" {
		resp.QueryErrorResp(c, "params is not null")
		return
	}

	list, err := category.GetCateByCateName(cname)
	if err != nil {
		resp.ServerErrorResp(c, resp.SERVER_ERR, "get data error")
		return
	}

	resp.SuccessResp(c, list)
}

// 根据名称搜索分类---模糊查
func GetCateLikeCateName(c *gin.Context) {
	cname := c.Query("name")

	if cname == "" {
		resp.QueryErrorResp(c, "params is not null")
		return
	}

	list, err := category.GetCateLikeCateName(cname)
	if err != nil {
		resp.ServerErrorResp(c, resp.SERVER_ERR, "get data error")
		return
	}

	resp.SuccessResp(c, list)
}
